package ups.apps.controladores;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@RequestScoped
public class LoginBean {
	
	private String usuario;
	private String clave;
	
	
	public String getUsuario() {
		return usuario;
	}
	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}
	public String getClave() {
		return clave;
	}
	public void setClave(String clave) {
		this.clave = clave;
	}
	
	public String login(){
		System.out.println("Logeando con usr: "+this.usuario+" pass: "+this.clave);
		logout();
		try {
			ExternalContext contx = FacesContext.getCurrentInstance().getExternalContext();
			HttpServletRequest req = (HttpServletRequest) contx.getRequest();
			req.login(this.usuario, this.clave);
			return "/public/menu.xhtml?faces-redirect=true";
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "/loginError.xhtml?faces-redirect=true";
		}
	}
	
	public String logout(){
		
		try {
			ExternalContext cntxt = FacesContext.getCurrentInstance().getExternalContext();
			HttpServletRequest req = (HttpServletRequest) cntxt.getRequest();			
			req.logout();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "/login.xhtml";
	}

}
